<?php
/**
 * 分类控制器
 * CategoryController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author: huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class CategoryController extends AdminController
{    
    protected $oModel;
    protected $_attributeLabels;
	public function __construct($id, $module) {
	    //模型
		$this->oModel = new AdminCategory();
        $this->_attributeLabels = $this->oModel->attributeLabels();
		return parent::__construct($id, $module);
	}
    //分类管理
	public function actionIndex(){
		$categoryData = $this->oModel->getCategoryData();
		$tree = new Tree($categoryData);
		$str="<tr id='list\$id'>               
                <td><input type='text' value='\$sort' class='width20 ajaxOperateSort' url='\$url' /></td>
                <td>\$id</td>
				<td>\$spacer\$cate_name &nbsp \$isnave</td>  
                <td>\$cate_type</td>
                <td>\$model_name</td>
                <td>\$items</td>
                <td>\$linkurl</td>
				<td>\$str_manage</td>
			</tr>";
		$categorys = $tree->get_tree(0, $str);
		$this->render('index',array('categorys'=>$categorys ));
	}
    /**
     * CategoryController::actionAdd()
     * 
     * @return void
     */
    public function actionAdd(){
	    $pid = $this->_request->getQuery('pid' ,0);
        $iCateType = $this->_request->getQuery('cate_type' ,1);
        if( isset($_POST) && $_POST ){
            $iCateType = isset($_POST['AdminCategory']['cate_type']) ? $_POST['AdminCategory']['cate_type'] : 1;
            //处理设置
            $setting = '';
            if( $iCateType!=3 ){
                $aData['setting'] = $_POST['setting'];
                $setting = Yii::app()->common->array2string($aData['setting']);
                unset($aData['setting']);
            }            
            //处理上传的栏目图标
            $aData['cate_image'] = isset($_POST['AdminCategory']['cate_image']) ? $_POST['AdminCategory']['cate_image'] : '';
            $iAid = isset($_POST['AdminCategory']['aid']) ? $_POST['AdminCategory']['aid'] : '';
            //修改附件已经被使用
            $iAid && AdminAttachment::updateData(array('isuse'=>1) ,'aid='.$iAid);
            $aData = $_POST['AdminCategory'];
            $aData['sort'] = 0;
            $aData['module'] = 'content';
            $aData['site_id'] = 1;
            $aData['setting'] = $setting;
            if( $iCateType!=3 ) {
                $mReturn = $this->oModel->saveData($aData ,false);
            } else {
                $mReturn = $this->oModel->saveData($aData);
            }            
            if( !is_array($mReturn) ){        
                
                if( $iCateType!=3 ) {
                    $aEidtData['url'] = $this->oModel->url = $this->createUrl('/content/lists' ,array('catid'=>$this->oModel->cate_id));
                }
                if($this->oModel->parent_id!=0){
					$parent = $this->oModel->getOne($this->oModel->parent_id);
					$pids = $parent['parent_ids'].$this->oModel->cate_id.',';
				}else{
					$pids = ','.$this->oModel->cate_id.',';
				}
                $aEidtData['sort'] = $this->oModel->cate_id;
                $aEidtData['parent_ids'] =   $pids;           
                $this->oModel->saveData($aEidtData ,false);
                $alert="SendWebMsg('success','菜单添加成功','".$this->createUrl('category/index')."')";
            } else {
                $alert="SendWebMsg('error','添加失败')";
            }
            
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
            
        }
        //获取站点所有可用模板
        $aTemplate = Yii::app()->admincommon->getTemplateList();
        $aTemplateList = array('0'=>'请选择');
        if( ! empty( $aTemplate ) ){
            foreach( $aTemplate as $aVal ){
                $aTemplateList[$aVal['dirname']] = $aVal['name'];
            }
        }
        if( $iCateType==1 ){
            $sTemplate = '_form';
        } elseif ($iCateType==2) {
            $sTemplate = '_formpage';
        } elseif ($iCateType==3) {
            $sTemplate = '_formlink';
        }
        $this->render($sTemplate ,array('aTemplateList'=>$aTemplateList ,'pid'=>$pid ,'iCateType'=>$iCateType));
    }
    /**
     * CategoryController::actionEdit() 编辑
     * 
     * @return void
     */
    public function actionEdit(){
        $pid = $this->_request->getQuery('pid' ,0);
        $iCateId = $this->_request->getQuery('cate_id' ,0);
        $this->oModel = $this->oModel->findByPk($iCateId);
        $iCateType = (isset($this->oModel->cate_type) && $this->oModel->cate_type) ? $this->oModel->cate_type : 1;
        if( isset($_POST) && $_POST ){
            $iCateType = isset($_POST['AdminCategory']['cate_type']) ? $_POST['AdminCategory']['cate_type'] : 1;
            //处理设置
            $setting = '';
            if( $iCateType!=3 ){
                $aData['setting'] = $_POST['setting'];
                $setting = Yii::app()->common->array2string($aData['setting']);
                unset($aData['setting']);
            }            
            //处理上传的栏目图标
            $aData['cate_image'] = isset($_POST['AdminCategory']['cate_image']) ? $_POST['AdminCategory']['cate_image'] : '';
            $iAid = isset($_POST['AdminCategory']['aid']) ? $_POST['AdminCategory']['aid'] : '';
            //修改附件已经被使用
            $iAid && AdminAttachment::updateData(array('isuse'=>1) ,'aid='.$iAid);
            if( isset($_POST['old']['cate_image']) && $_POST['old']['cate_image'] ){
                AdminAttachment::updateData(array('isuse'=>0) ,'filepath="'.$_POST['old']['cate_image'].'"');
            }
            $aData = $_POST['AdminCategory'];
            $aData['sort'] = 0;
            $aData['module'] = 'content';
            $aData['site_id'] = 1;
            $aData['setting'] = $setting;
            if( $iCateType!=3 ) {
                $mReturn = $this->oModel->saveData($aData ,false);
            } else {
                $mReturn = $this->oModel->saveData($aData);
            }            
            if( !is_array($mReturn) ){        
                
                if( $iCateType!=3 ) {
                    $aEidtData['url'] = $this->oModel->url = $this->createUrl('/content/lists' ,array('catid'=>$this->oModel->cate_id));
                }
                if($this->oModel->parent_id!=0){
					$parent = $this->oModel->getOne($this->oModel->parent_id);
					$pids = $parent['parent_ids'].$this->oModel->cate_id.',';
				}else{
					$pids = ','.$this->oModel->cate_id.',';
				}
                $aEidtData['sort'] = $this->oModel->cate_id;
                $aEidtData['parent_ids'] =   $pids;         
                $this->oModel->saveData($aEidtData ,false);
                $alert="SendWebMsg('success','栏目成功','".$this->createUrl('category/index')."')";
            } else {
                $alert="SendWebMsg('error','编辑失败')";
            }
            
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
            
        }
        //获取站点所有可用模板
        $aTemplate = Yii::app()->admincommon->getTemplateList();
        $aTemplateList = array('0'=>'请选择');
        if( ! empty( $aTemplate ) ){
            foreach( $aTemplate as $aVal ){
                $aTemplateList[$aVal['dirname']] = $aVal['name'];
            }
        }
        //获取栏目相关设置
        $setting = Yii::app()->common->string2array($this->oModel->setting);
        //echo '<pre>';
        //print_r($aData);exit;
        //$this->render('_form', array('aTemplateList'=>$aTemplateList ,'aTemplate'=>$aData));
        if( $iCateType==1 ){
            $sTemplate = '_form';
        } elseif ($iCateType==2) {
            $sTemplate = '_formpage';
        } elseif ($iCateType==3) {
            $sTemplate = '_formlink';
        }
        $this->render($sTemplate ,array('aTemplateList'=>$aTemplateList ,'pid'=>$pid ,'iCateType'=>$iCateType ,'setting'=>$setting));
    }
    
    /**
     * ModelController::actionAjaxTemplateList() 获取风格下模板
     * 
     * @return void
     */
    public function actionAjaxTemplateList(){
        $aResulte = $aData = $aHtml = array();
        $sTemplate = $this->_request->getQuery('sTemplate');
        $aResulte = 
        $aData = Yii::app()->admincommon->getHandleTemplateList($sTemplate);
        if( ! empty( $aData['category'] ) ){
            //根据对应栏目生成HTML
            $aHtml['category'] = Form::select($aData['category'] ,0 ,"name='setting[category_template]'");
            $aHtml['list'] = Form::select($aData['list'] ,0 ,"name='setting[list_template]'");
            $aHtml['show'] = Form::select($aData['show'] ,0 ,"name='setting[show_template]'");
            $aHtml['page'] = Form::select($aData['page'] ,0 ,"name='setting[page_template]'");
            $aResulte['err'] = 0;
            $aResulte['msg'] = 'success';
            $aResulte['data']  = $aHtml;
        } else {
            $aResulte['err'] = 1;
            $aResulte['msg'] = 'error';
        }      
        die(json_encode($aResulte));
        
    }
    /**
     * ModuleController::actionajaxSort() 排序
     * 
     * @return void
     */
    public function actionAjaxSort(){
        $aResulte = array();
        $iCateId = $this->_request->getQuery('cate_id');
        $iSort = $this->_request->getQuery('sort');
        $this->oModel = $this->oModel->findByPk($iCateId);
        $aData = $this->oModel->attributes;
        if( ! empty( $aData ) ){
            $aData['sort'] = $iSort;
            $mResult = $this->oModel->saveData($aData);
            if( is_string($mResult) ){
                $aResulte = array(
                    'err' => 0 ,
                    'msg' => '排序成功'
                );
            } else {
                $aResulte = array(
                    'err' => 1 ,
                    'msg' => '排序失败'
                );
            }
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '排序失败，可能原因是数据已经被删除了.'
            );
        }
        die(json_encode($aResulte));
    }
    /**
     * ModuleController::actionDelete()
     * 
     * @return void
     */
    public function actionDelete(){
        $aResult = array();
        $iCateId = $this->_request->getQuery('cate_id');
        $mResult = $this->oModel->deleteData($iCateId);
        if( $mResult ){
            $this->ajaxReturn(0,'删除成功',$this->createUrl('category/index'));
        } else {
            $this->ajaxReturn(1,'删除失败');
        }
    }
}